/**
 * 
 */
package org.swing.utility.net.http;

import javax.naming.ServiceUnavailableException;

import java.io.IOException;

import org.apache.http.client.methods.HttpRequestBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swing.utility.net.core.AdjustableSemaphore;

/**
 * @author lqnhu
 *
 */
public class ConstrainedHttpClient {
	private static final Logger logger = LoggerFactory.getLogger(ConstrainedHttpClient.class);
	protected AdjustableSemaphore semaphore;

	public ConstrainedHttpClient(int maxConcurrentRequests) {
		semaphore = new AdjustableSemaphore(maxConcurrentRequests);
	}




	/**
	 * @return Returns the number of outstanding requests that have not returned
	 *         yet from http client.
	 */
	public int getCurrentRequests() {
		return semaphore.maxPermits() - semaphore.availablePermits();
	}

	/**
	 * Resets the maximum allowed concurrent requests
	 *
	 * @param maxConcurrentRequests
	 *            - the new maximum
	 */
	public void setMaxConcurrentRequests(int maxConcurrentRequests) {
		semaphore.setMaxPermits(maxConcurrentRequests);
	}
}
